﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COC_helper
{
    public partial class SlavePla : Form
    {
        public SlavePla()
        {
            InitializeComponent();
        }



 

        private void SlavePla_Shown(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(255, 244, 192);
            Debug.WriteLine("开始读取数据:");
            foreach (var item in Global_Value.zmb_Setting.slaveLists)
            {
                Debug.WriteLine(item.Name.ToString());
                listBox1.Items.Add(item.Name+" "+item.HP+" thing: "+item.ThingList+" state: " + item.StateList);
                
                
            }
        }
        /// <summary>
        /// 从界面上读取数据并保存到列表中
        /// </summary>
        protected void SaveNewSlave()
        {
            if (TextBox_Name.Text == "")
            {
                MessageBox.Show("您至少需要输入随从的姓名");
                return;
            }
            SlaveList slaveList = new SlaveList();
            slaveList.Name = TextBox_Name.Text;
            slaveList.HP = TextBox_HP.Text;
            slaveList.StateList = TextBox_StateList.Text;
            slaveList.ThingList = TextBox_ThingList.Text;
            listBox1.Items.Add(slaveList.Name + " " + slaveList.HP + " thing: " + slaveList.ThingList + " state: " + slaveList.StateList);
            Global_Value.zmb_Setting.slaveLists.Add(slaveList);
        }
        /// <summary>
        /// 删除元素
        /// </summary>
        protected void DelSlave()
        {

            if (listBox1.SelectedItem == null)
            {
                Debug.WriteLine("list指向异常！");
                return;
            }
            //listBox1.Items.Add(item.Name+" "+item.HP+" thing: "+item.ThingList+" state: " + item.StateList);
            string[] temp = listBox1.SelectedItem.ToString().Split(' ');
            listBox1.Items.Remove(listBox1.SelectedItem);
            try
            {
                foreach (var item in Global_Value.zmb_Setting.slaveLists)
                {
                    if (item.Name == temp[0])
                        Global_Value.zmb_Setting.slaveLists.Remove(item);

                }

            }
            catch (Exception)
            {

              
            }


        }
        

        private void Button_CreatNew_Click(object sender, EventArgs e)
        {
            if (Button_CreatNew.Tag.ToString() == "creat")
            {
                TextBox_ThingList.Text = TextBox_StateList.Text = TextBox_HP.Text = TextBox_Name.Text = "";
                Button_Del.Visible = Button_Save.Visible = false;
                Button_CreatNew.Text = "保存当前修改";
                Button_CreatNew.Tag = "save";
            }
            else //注意没有名字的属性不能保存
            {
                SaveNewSlave();
                Button_CreatNew.Text = "创建新的随从";
                Button_CreatNew.Tag = "creat";
            }
            
        }

        private void Button_Save_Click(object sender, EventArgs e)
        {
            DelSlave();
            SaveNewSlave();

        }

        private void Button_Del_Click(object sender, EventArgs e)
        {
            DelSlave();
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null || listBox1.SelectedItem.ToString() == "")
                return;
            try
            {
                Button_Del.Visible = Button_Save.Visible = true;
                string[] temp = listBox1.SelectedItem.ToString().Split(' ');
                TextBox_Name.Text = temp[0];
                TextBox_HP.Text = temp[1];
                TextBox_ThingList.Text = temp[3];
                TextBox_StateList.Text = temp[5];
            }
            catch (Exception)
            {

               
            }


        } 
    }
    
}
